package sg.nus.iss.vmcs.dao;

import sg.nus.iss.vmcs.VMCSException;
import sg.nus.iss.vmcs.models.CashStoreItem;
import sg.nus.iss.vmcs.models.Coin;
import sg.nus.iss.vmcs.models.AbstractStoreFactory;
import sg.nus.iss.vmcs.utils.StoreType;

/**
 * Property Loader for Cash Store
 * @author Pang Pingli
 */
public class CashPropertyLoader extends PropertyLoader {
	
	/**
	 * Creates an Instance of the Cash Property Loader
	 * @param filen File Name to load from
	 */
	public CashPropertyLoader(String filen) {
		super(filen);
	}

	/**
	 * Retrieves a Cash Store Item
	 * @param idx Index of the item to retrieve
	 * @return Instance of a CashStoreItem
	 */
	public CashStoreItem getItem(int idx){
	    
		//Use factory to create store objects and items.
		AbstractStoreFactory dsFactory = AbstractStoreFactory.getStoreFactory(StoreType.CASH);
		Coin cn = (Coin)dsFactory.makeStoreObject();
	
	    String item = new String("Name" + idx);
	    String itemv = getValue(item);
	    cn.setName(itemv);
	
	    item = new String("Weight" + idx);
	    itemv = getValue(item);
	    cn.setWeight(Double.parseDouble(itemv));
	
	    item = new String("Value" + idx);
	    itemv = getValue(item);
	    cn.setValue(Integer.parseInt(itemv));
	
	    item = new String("Quantity" + idx);
	    itemv = getValue(item);
	
		try {
			 CashStoreItem cit = (CashStoreItem) dsFactory.makeStoreItem(cn, Integer.parseInt(itemv));
			    return cit;
		} 
		catch (NumberFormatException e) {
			e.printStackTrace();
		} 
		catch (VMCSException e) {
			e.printStackTrace();
		}
		
		return null;
	}

	/**
	 * Sets a Cash Store Item
	 * @param it Index of Cash Store Item to set
	 * @param item Cash Store Item to set
	 */
	public void setItem(int it, CashStoreItem item){
	    
		int idx = it + 1;
	    Coin cn = (Coin) item.getContent();
	    String itn = new String("Name" + idx);
	
	    setValue(itn, cn.getName());
	
	    itn = new String("Weight" + idx);
	    setValue(itn, String.valueOf(cn.getWeight()));
	
	    itn = new String("Value" + idx);
	    setValue(itn, String.valueOf(cn.getValue()));
	
	    itn = new String("Quantity" + idx);
	    setValue(itn, String.valueOf(item.getQuantity()));
	}
}